package com.zhuojy.equity.mall.admin.web.controller.quartz;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.zhuojy.equity.mall.admin.common.entity.bo.salary.JobMessageBO;
import com.zhuojy.equity.mall.admin.common.entity.po.salary.SalaryWhoPO;
import com.zhuojy.equity.mall.admin.common.pojo.QuartzParam;
import com.zhuojy.equity.mall.admin.common.pojo.ResultEntity;
import com.zhuojy.equity.mall.admin.common.utils.CronUtils;
import com.zhuojy.equity.mall.admin.service.quartz.QuartzServiceImpl;
import com.zhuojy.equity.mall.admin.service.salary.SalaryWhoService;
import lombok.RequiredArgsConstructor;
import org.quartz.JobDataMap;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @description: quartz控制类
 * @author chen
 * @date 2024/4/15 19:27
 * @version 1.0
 */

@RestController
@RequestMapping("/quartz")
@RequiredArgsConstructor
public class QuartzController {

    @Autowired
    private QuartzServiceImpl quartzService;
    @Autowired
    private Scheduler scheduler;

    private final SalaryWhoService salaryWhoService;

    @PostMapping("/addJob")
    public ResultEntity addJob(@RequestBody List<JobMessageBO> jobMessageBO, @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam("sendTime") String sendTime) {
        try {
            for (JobMessageBO messageBO : jobMessageBO) {
                QuartzParam param = new QuartzParam();
                DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                //测试数据
                JobDataMap jobDataMap = new JobDataMap();
                jobDataMap.put("meetingId", UUID.randomUUID().toString().replace("-", ""));
                jobDataMap.put("meetingName", "邮箱发送");
                jobDataMap.put("startTime", new Date());
                jobDataMap.put("salaryId",messageBO.getSalaryId());
                jobDataMap.put("userId",messageBO.getUserId());
                jobDataMap.put("meetingPeople", messageBO.getName());

                param.setJobName(messageBO.getUserId().toString());
                param.setJobGroup(messageBO.getName());
                param.setJobDataMap(jobDataMap);
    //            param.setJobClassName("com.feng.controller.Meeting.addMeeting()");
                param.setCron(CronUtils.toCronExpression(LocalDateTime.parse(sendTime, fmt)));   //1分钟执行一次
                Boolean flag = quartzService.addJob(param, messageBO.getUserId().toString());
                // 将状态改为预约发送
                if (flag){
                    salaryWhoService.update(Wrappers.lambdaUpdate(SalaryWhoPO.class).eq(SalaryWhoPO::getUserId,messageBO.getUserId()).eq(SalaryWhoPO::getSalaryId,messageBO.getSalaryId()).set(SalaryWhoPO::getIssuanceStatus,2));
                }
            }
            return ResultEntity.OK("预约成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.ERROR("预约失败");
        }


    }
    @RequestMapping("/deleteJob")
    public ResultEntity deleteJob(@RequestBody List<JobMessageBO> jobMessageBO) throws SchedulerException {
        try {
            for (JobMessageBO messageBO : jobMessageBO) {
                boolean meeting1 = scheduler.checkExists(JobKey.jobKey(messageBO.getUserId().toString(),messageBO.getName()));
                if (meeting1){
                    scheduler.deleteJob(JobKey.jobKey(messageBO.getUserId().toString(),messageBO.getName()));
                    salaryWhoService.update(Wrappers.lambdaUpdate(SalaryWhoPO.class).eq(SalaryWhoPO::getSalaryId,messageBO.getSalaryId()).eq(SalaryWhoPO::getUserId,messageBO.getUserId()).set(SalaryWhoPO::getIssuanceStatus,3));
                }
            }
            return ResultEntity.OK("撤回成功");
        } catch (SchedulerException e) {
            e.printStackTrace();
            return ResultEntity.ERROR("撤回失败");
        }
    }
}
